<?php
/**
 * Created by PhpStorm.
 * User: edmmss
 * Date: 2018/12/5
 * Time: 13:55
 */

namespace app\modules\flyweight\controller;

use app\modules\flyweight\FlyweightFactory;
use yii\web\Controller;

class DefaultController extends Controller
{
    public $characters = ['a', 'b', 'c', 'd', 'e'];
    public $fonts = ['e', 'd', 'm', 'm', 's', 's'];

    /**
     * 享元模式示例
     * 为了节约内存的使用，享元模式会尽量使类似的对象共享内存。在大量类似对象被使用的情况中这是十分必要的。
     * 常用做法是在外部数据结构中保存类似对象的状态，并在需要时将他们传递给享元对象。
     *
     * @date   2018/12/5
     * @author edmmss
     * @return bool
     */
    public function actionIndex()
    {
        $factory = new FlyweightFactory();
        foreach ($this->characters as $character) {
            foreach ($this->fonts as $font) {
                $characterFlyweight = $factory->get($character);
                $render = $characterFlyweight->render($font);

                var_dump($render);
            }
        }

        return true;
    }
}